<?php
/**
 * Abstract class for list filter data models
 *
 * @author Lukas Normantas
 */
abstract class Areal_Model_CollectionFilter implements Areal_Model_ICollectionFilter
{
    public function getNamespaceAlias()
    {
        return get_class($this);
    }

    public function setData($data)
    {
        $form = $this->getForm();
        if ($form->isValid( $data )){
            $namespace = new Zend_Session_Namespace($this->getNamespaceAlias());
            $namespace->data = $form->getValues();
            return true;
        }
    }

    public function getData()
    {
        $namespace = new Zend_Session_Namespace($this->getNamespaceAlias());
        return $namespace->data ? $namespace->data : $this->getDefaultData();
    }

    public function setSort($field)
    {
        $current_sort = $this->getSort();
        if ($current_sort == $field . ' ASC') {
            $sort = $field . ' DESC';
        } else {
            $sort = $field . ' ASC';
        }
        $namespace = new Zend_Session_Namespace($this->getNamespaceAlias());
        $namespace->sort = $sort;
    }

    public function getSort()
    {
        $namespace = new Zend_Session_Namespace($this->getNamespaceAlias());
        return $namespace->sort ? $namespace->sort : $this->getDefaultSort();
    }
}